package com.gitee.cui.netty.encoder;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.util.List;

/**
 * 字符串转整型编码器
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/10/25 10:22
 **/
public class String2IntegerEncoder extends MessageToMessageEncoder<String> {

    @Override
    protected void encode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {
        char[] chars = msg.toCharArray();
        for (char c : chars) {
            if (c >= 48 && c <= 57) {
                // 数字0-9
                out.add(new Integer(c - 48));
            }
        }
    }
}
